package yns.springcloud.oakwood.guide;

import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

//单节点
//SpringCloudService1  http://localhost:9100
//actuator      http://localhost:9100/actuator
//配置文件的info	http://localhost:9100/actuator/info

//本机多节点需要在 hosts文件中加入对应的eureka.instance.hostname

//项目端口记录
//SpringCloudGuideService-Single    server.port=9100

//SpringCloudService-Multiple-1     server.port=9101
//SpringCloudService-Multiple-2     server.port=9102

//SpringCloudService-Multiple-3     server.port=9103
//SpringCloudService-Multiple-4     server.port=9104
//SpringCloudService-Multiple-5     server.port=9105

//SpringCloudGuideProducer1         server.port=9200
//SpringCloudGuideProducer2         server.port=9201

//SpringCloudGuideConsumer1         server.port=9300
//SpringCloudGuideConsumer2         server.port=9301

//SpringCloudGuideConfigServer1     server.port=9500
//SpringCloudGuideConfigServer2     server.port=9501
//SpringCloudGuideConfigClient1     server.port=9510

//SpringCloudGuideGateway1          server.port=9400
//SpringCloudGuideGateway2          server.port=9401



@EnableEurekaServer
@SpringBootApplication
public class SpringCloudSetup {

	private static final Logger logger = LoggerFactory.getLogger(SpringCloudSetup.class);

	private static final String SPRING_APPLICATION_NAME = "spring.application.name";
	private static final String SERVER_HOSTNAME = "server.hostname";
	private static final String SERVER_PORT = "server.port";
	private static final String EUREKA_INSTANCE_HOSTNAME = "eureka.instance.hostname";
	private static final String EUREKA_CLIENT_SERVICEURL_DEFAULTZONE = "eureka.client.serviceUrl.defaultZone";

	private static final String DEFAULT_MESSAGE = "Does Key exist or null ?";

	public static void main(String[] args) throws Exception {
		ConfigurableApplicationContext configurableApplicationContext = SpringApplication.run(SpringCloudSetup.class,
				args);
		Environment environment = configurableApplicationContext.getEnvironment();

		logEnvironment(environment);
	}

	private static void logEnvironment(Environment environment) {
		Object[] objects = new Object[5];
		String format = "Environment Configure : \n" 
				+ "spring.application.name : {} \n" 
				+ "server.hostname : {} \n"
				+ "server.port : {} \n" 
				+ "eureka.instance.hostname : {} \n"
				+ "eureka.client.serviceUrl.defaultZone : {} \n";

		objects[0] = Optional.ofNullable(environment.getProperty(SPRING_APPLICATION_NAME)).orElse(DEFAULT_MESSAGE);
		objects[1] = Optional.ofNullable(environment.getProperty(SERVER_HOSTNAME)).orElse(DEFAULT_MESSAGE);
		objects[2] = Optional.ofNullable(environment.getProperty(SERVER_PORT)).orElse(DEFAULT_MESSAGE);
		objects[3] = Optional.ofNullable(environment.getProperty(EUREKA_INSTANCE_HOSTNAME)).orElse(DEFAULT_MESSAGE);
		objects[4] = Optional.ofNullable(environment.getProperty(EUREKA_CLIENT_SERVICEURL_DEFAULTZONE))
				.orElse(DEFAULT_MESSAGE);

		logger.info(format, objects);
	}

}
